package com.randi.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.randi.common.annotation.LogAnnotation;
import com.randi.common.core.domain.Result;
import com.randi.common.enums.BusinessType;
import com.randi.common.enums.ResultCode;
import com.randi.common.utils.page.PageHelperUtils;
import com.randi.system.domain.entity.sysDict.SysDictData;
import com.randi.system.domain.params.sysDict.SysDictDataParam;
import com.randi.system.service.sysDict.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ziye
 */
@RestController
@RequestMapping("system/dict/data")
@Api(tags = "字典数据")
public class SysDictDataController {

    @Resource
    private ISysDictDataService iSysDictDataService;

    /**
     * 分页获取列表
     *
     * @param sysDictDataParam 检索参数
     * @return Result
     */
    @PostMapping("/list")
    @ApiOperation(value = "获取列表", notes = "")
    @PreAuthorize("@auth.hasPermission('system:dict:data:list')")
    public Result selectListByDictId(@RequestBody SysDictDataParam sysDictDataParam) {
        PageHelperUtils.initPage(sysDictDataParam);
        PageInfo<SysDictData> dictDataList = iSysDictDataService.selectPageList(sysDictDataParam);
        return Result.success().putPage(dictDataList);
    }

    /**
     * 查看详情
     * @param dictDataId dictDataId
     * @return Result
     */
    @GetMapping("/detail/{dictDataId}")
    @ApiOperation(value = "查看详情", notes = "")
    @PreAuthorize("@auth.hasPermission('system:dict:data:list')")
    public Result selectById(@PathVariable Long dictDataId) {
        SysDictData sysDictData = iSysDictDataService.selectSysDictDataById(dictDataId);
        return Result.success().put(sysDictData);
    }

    /**
     * 插入数据
     *
     * @param sysDictData 业务信息
     * @return 结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "插入数据", notes = "")
    @PreAuthorize("@auth.hasPermission('system:dict:data:add')")
    @LogAnnotation(module = "字典数据", value = "插入数据", businessType = BusinessType.UPDATE)
    public Result insert(@Validated @RequestBody SysDictData sysDictData) {
        int status = iSysDictDataService.insertSysDictData(sysDictData);

        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_INSERT_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_INPUT_SUCCESS);
    }

    /**
     * 更新数据
     *
     * @param sysDictData 业务信息
     * @return 结果
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新数据", notes = "")
    @PreAuthorize("@auth.hasPermission('system:dict:data:edit')")
    @LogAnnotation(module = "字典数据", value = "更新数据", businessType = BusinessType.UPDATE)
    public Result update(@Validated @RequestBody SysDictData sysDictData) {
        int status = iSysDictDataService.updateSysDictData(sysDictData);

        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_EDITOR_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_EDITOR_SUCCESS);
    }

    /**
     * 删除数据
     *
     * @param dictDataIds dictDataIds
     * @return 结果
     */
    @PostMapping("/delete")
    @ApiOperation(value = "批量删除", notes = "")
    @PreAuthorize("@auth.hasPermission('system:dict:data:delete')")
    @LogAnnotation(module = "字典数据", value = "删除数据", businessType = BusinessType.DELETE)
    public Result deleteBatch(@RequestBody List<Long> dictDataIds) {
        int status = iSysDictDataService.deleteBatchSysDictData(dictDataIds);

        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_DELETE_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_DELETE_SUCCESS);
    }

}
